package com.luxshare.aos.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.luxshare.aos.R;
import com.luxshare.aos.demo.service.FileService;

/**
 * 1、添加访问SD卡权限：
 * <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 * 2、在SDCard指定目录下创建文件
 */
public class WriteSDCARDActivity extends Activity implements View.OnClickListener {
    private EditText tvFileName;
    private EditText tvFileContent;
    private Button createButton;
    private FileService fileService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_sdcard);

        tvFileName = (EditText) findViewById(R.id.file_name);
        tvFileContent = (EditText) findViewById(R.id.file_content);
        createButton = (Button) findViewById(R.id.create_button);

        tvFileName.setText("WriteSDCARD.txt");
        tvFileContent.setText("<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>\n<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"/>");
        createButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String fileName = tvFileName.getText().toString();
        String fileContent = tvFileContent.getText().toString();
        fileService = new FileService(this);
        try {
            fileService.save(fileName, fileContent);
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                fileService.saveToSDCard(fileName, fileContent);
                Toast.makeText(this, "保存成功", Toast.LENGTH_LONG);
            } else {
                Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_LONG);
            }
        } catch (Exception e) {
            Log.i(getClass().getName(), e.getMessage());
        }
    }

}